/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ttfsserver;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import pojo.POJOInfoClient;
import myutil.XMLHelper;

/**
 *
 * @author Tidus Le
 */
public class clsXMLInfoClient
{
    /**
     * Đọc thông tin của các Client từ file xml
     */
    public static void read(ArrayList<POJOInfoClient> arrInfoClients)
    {
        ArrayList<Hashtable<String,String>> arrElements=XMLHelper.read("InfoClients");
        
        for(Hashtable<String,String> element:arrElements)
        {
            POJOInfoClient infoC=new POJOInfoClient();
            Enumeration e=element.keys();
              while(e.hasMoreElements())
              {
                  String key=(String)e.nextElement();
                  if(key.equals("id")==true)
                      infoC.setId(element.get(key));
                  else
                      if(key.equals("ip")==true)
                        infoC.setIp(element.get(key));
                      else
                          if(key.equals("port")==true)
                            infoC.setPort(element.get(key));
                          else
                              if(key.equals("name")==true)
                                infoC.setName(element.get(key));
                              else
                                  if(key.equals("status")==true)
                                    infoC.setStatus(element.get(key));
              }
            arrInfoClients.add(infoC);
        }
    }
    
    /**
     * Ghi những thông tin của client xuống đĩa cứng
     */
    public static void write(ArrayList<POJOInfoClient> arrInfoClients)
    {
        ArrayList<Hashtable<String,String>> arrElements=new ArrayList<Hashtable<String, String>>();
        
        for(POJOInfoClient ic:arrInfoClients)
        {
            Hashtable<String,String> element=new Hashtable<String, String>();
            element.put("id", ic.getId());
            element.put("ip", ic.getIp());
            element.put("port", ic.getPort());
            element.put("name", ic.getName());
            element.put("status", ic.getStatus());
            arrElements.add(element);
        }
        XMLHelper.write("InfoClients", arrElements);
    }
}
